<template>
  <div class="zd-healthy">

    <el-tabs v-model="userType" type="card">
      <el-tab-pane label="孕前期" name="1">
      </el-tab-pane>
      <el-tab-pane label="怀孕期" name="2">
      </el-tab-pane>
      <el-tab-pane label="儿童期（CH1）" name="3">
      </el-tab-pane>
      <el-tab-pane label="儿童期（CH2）" name="4">
      </el-tab-pane>
    </el-tabs>

    <!-- 查询区域 -->
    <el-form ref="form" :model="searchData" label-width="120px" class="clearfix">
      <!-- 操作区域 -->
      <div class="pull-left pd-bt">
        <el-button type="primary" @click="queryHandler" class=" pd-bt">添加</el-button>
      </div>
    </el-form>

    <!---->
    <template>
      <el-table   :data="tableData" border empty-text="当前列表为空" v-loading="loading" element-loading-text="加载中" style="width: 100%">
        <el-table-column align="center" label="编号" width="60">
          <template slot-scope="scope">
            {{(pageCount-1)*pageSize+scope.$index+1}}
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="" prop="ItemName" label="健康现况描述" min-width="400">
        </el-table-column>

        <el-table-column min-width="100" align="center" label="操作">
          <template slot-scope="scope">
            <!-- <template slot-scope="scope"> -->
            <el-button size="small" type="text" @click="changePositionHandler(1,scope.$index, scope.row)">
              <span class="arrow-top"></span>
            </el-button>
            <el-button size="small" type="text" @click="changePositionHandler(-1,scope.$index, scope.row)">
              <span class="arrow-bottom"></span>
            </el-button>
            <el-button size="small" type="text" @click="queryHandler(scope.$index, scope.row)">查看</el-button>
            <el-button size="small" type="text" @click="deleteHandler(scope.$index, scope.row)">删除</el-button>
            <!-- </template> -->
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--分页-->
    <div class="block mg-20-auto">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageCount" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 编辑查看对话框 -->
    <el-dialog :title="dialogTitle" :close-on-click-modal="false"  v-loading="saveloading" element-loading-text="正在保存" :visible.sync="centerDialogVisible" width="50%">

      <el-form :model="form" :rules="formRules" ref="healthyForm">
        <el-form-item label="健康现况描述" :label-width="formLabelWidth" prop="ItemName">
          <el-input v-model="form.ItemName" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveHandler">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "healthy1",
  data() {
    return {
      searchData: {
        name: "",
        number: ""
      },
      dialogTitle: "",
      form: {
        ItemName: "",
        UserType: this.userType,
        SortIndex: ""
      },
      userType: "1",
      formRules: {
        ItemName: [
          { required: true, message: "请填写健康现况", trigger: "blur" }
        ],
        cscode: [{ required: true, message: "请填写地点编号", trigger: "blur" }]
      },
      centerDialogVisible: false,
      formLabelWidth: "120px",
      total: null,
      pageSize: 10,
      pageCount: 1,
      loading: false,
      saveloading: false,
      tableData: [],
      data1: [],
      data2: [],
      maxHeight:''
    };
  },
  watch: {
    userType(value) {
      this.pageCount = 1;
      this.getList();
      if (value == 1) {
        this.tableData = this.data1;
      } else {
        this.tableData = this.data2;
      }
    }
  },
  mounted() {},
beforeMount:function() {
  let that=this;
  let appHeight=$(window).height();
  let autoheight=(appHeight-310)+'px'
  that.maxHeight=autoheight
},
  methods: {
    changePositionHandler(direction, index, row) {
      let tmpIndex;
      let tmpItem;
      if (direction == 1) {
        // 排在第一的不可以在向上
        if (index <= 0) {
          return;
        }
        tmpIndex = index - 1;
        tmpItem = this.tableData[tmpIndex];
        if (!tmpItem) {
          tmpIndex = this.tableData.length - 1;
          tmpItem = this.tableData[tmpIndex];
        }
      } else {
        // 排在最下面的不在刻意向下
        if (index >= this.tableData.length - 1) {
          return;
        }
        tmpIndex = index + 1;
        tmpItem = this.tableData[tmpIndex];
        if (!tmpItem) {
          tmpIndex = 0;
          tmpItem = this.tableData[tmpIndex];
        }
      }
      tmpIndex+=(this.pageCount-1)*10
      index+=(this.pageCount-1)*10
      this.changeSort(row, tmpIndex);
      this.changeSort(tmpItem, index);
    },
    changeSort(item, index) {
      let that = this;
      this.form.UserType = this.userType;
      that
        .service({
          url: "Dic/SaveHealthConditionItem",
          method: "post",
          data: {
            ...item,
            SortIndex: index
          }
        })
        .then(resp => {
           that.getList();
          // that.tableData.push(that.form);
        })
        .catch(data => {
           that.getList();
          that.loading = false;
        });
    },
    queryHandler(index, row) {
      if (row) {
        this.dialogTitle = "健康现状描述";
        this.form = JSON.parse(JSON.stringify(row));
      } else {
        this.dialogTitle = "健康现状描述";
        this.form = {
          ItemName: "",
          UserType: this.userType
        };

        this.$nextTick(() => {
          this.$refs.healthyForm.resetFields();
        });
      }
      this.centerDialogVisible = true;
    },
    deleteHandler(index, row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let that = this;
          that
            .service({
              url: "Dic/DeleteHealthConditionItem?sysId=" + row.SYS_ID,
              method: "post"
            })
            .then(resp => {
              this.$message({
                type: "success",
                message: "删除成功!"
              });

              that.getList();
              // that.tableData.push(that.form);
            })
            .catch(data => {
              that.loading = false;
            });
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      //条数改变
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      //页数改变
      this.pageCount = val;
      this.getList();
    },
    getList() {
      let that = this;
      this.loading = true;
      that
        .service({
          url: "Dic/GetHealthConditionItem",
          method: "get",
          params: {
            userType: that.userType,
            pageCount: that.pageCount - 1,
            pageSize: that.pageSize
          }
        })
        .then(response => {
          that.tableData = response.dtData.concat();
          if (that.userType == 1) {
            that.data1 = response.dtData.concat();
          } else {
            that.data2 = response.dtData.concat();
          }
          that.total = parseInt(response.dtData[0].TotalCount || 0);
          that.loading = false;
        })
        .catch(data => {
          that.loading = false;
        });
    },
    searchHandler() {
      this.pageCount = 1; // 分页索引
      this.getList();
    },
    saveHandler() {
      let that = this;
      this.form.UserType = this.userType;
      this.$refs.healthyForm.validate(valid => {
        if (valid) {
          this.saveloading = true;

          that
            .service({
              url: "Dic/SaveHealthConditionItem",
              method: "post",
              data: {
                ...that.form,
                SortIndex: that.form.SortIndex?that.form.SortIndex:(that.total+1)
              }
            })
            .then(resp => {
              that.getList();
              that.centerDialogVisible = false;
              that.saveloading = false;
            })
            .catch(data => {
              that.getList();
              that.loading = false;
              that.saveloading = false;
            });
        }
      });
    }
  },
  mounted() {
    this.getList();
  },
  components: {}
};
</script>
<style lang="scss">
.zd-healthy {
}

.arrow-top {
  position: relative;
  display: inline-block;
  box-sizing: border-box;
  width: 8px;
  height: 8px;
  border: 1px solid #409eff;
  border-width: 1px 0 0 1px;
  transform: rotate(45deg) translateY(-50%);
  &::before {
    content: "";
    position: absolute;
    height: 1px;
    width: 12px;
    background-color: #409eff;
    left: 0;
    top: -1px;
    transform: rotate(45deg);
    transform-origin: 0 0;
  }
}

.arrow-bottom {
  position: relative;
  display: inline-block;
  box-sizing: border-box;
  width: 8px;
  height: 8px;
  border: 1px solid #409eff;
  border-width: 0 1px 1px 0;
  transform: rotate(45deg);
  &::before {
    content: "";
    position: absolute;
    height: 1px;
    width: 12px;
    background-color: #409eff;
    right: 0;
    bottom: 0;
    transform: rotate(45deg);
    transform-origin: 100% 0;
  }
}

.zd-healthy .el-dialog__wrapper .el-dialog .el-dialog__footer {
  text-align: center;
}
</style>
